﻿using Edge_tts_sharp;
using Microsoft.AspNetCore.Mvc;
using XinLife.Core.Model;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace XinLife.Controllers.Business
{
    /// <summary>
    /// tts语音
    /// </summary>
    public class TTSController : BaseController
    {
        /// <summary>
        /// 获取中文语音
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<AjaxResult> GetVoices()
        {
            var all_voices = Edge_tts.GetVoice();
            var voices = all_voices.FindAll(t=>t.Locale.StartsWith("zh"));
            foreach (var vo in voices)
            {
                string name = "";
                switch (vo.ShortName)
                {
                    case "zh-CN-XiaoxiaoNeural":
                        name = "晓晓";
                        break;
                    case "zh-CN-XiaoyiNeural":
                        name = "晓伊 儿童";
                        break;
                    case "zh-CN-YunjianNeural":
                        name = "云健 中年人";
                        break;
                    case "zh-CN-YunxiNeural":
                        name = "云希 男-年轻人";
                        break;
                    case "zh-CN-YunxiaNeural":
                        name = "云夏 男-儿童";
                        break;
                    case "zh-CN-YunyangNeural":
                        name = "云扬 男-年轻人";
                        break;
                    case "zh-CN-shaanxi-XiaoniNeural":
                        name = "晓妮（陕西）";
                        break;
                    case "zh-CN-liaoning-XiaobeiNeural":
                        name = "晓北（辽宁）";
                        break;
                    case "zh-TW-HsiaoChenNeural":
                        name = "曉臻（台湾）";
                        break;
                    case "zh-TW-YunJheNeural":
                        name = "雲哲（台湾）";
                        break;
                    case "zh-TW-HsiaoYuNeural":
                        name = "曉雨（台湾）";
                        break;
                    case "zh-HK-HiuGaaiNeural":
                        name = "曉佳（香港）";
                        break;
                    case "zh-HK-HiuMaanNeural":
                        name = "曉曼（香港）";
                        break;
                    case "zh-HK-WanLungNeural":
                        name = "雲龍（香港）";
                        break;
                    default:
                        name = vo.ShortName;
                        break;
                }
                vo.Name = name;
            }
            return Success(voices.Where(t => !t.Name.Contains("（香港）")).Select(t => new { cId = t.ShortName, cName = t.Name }));
        }
        /// <summary>
        /// 文字转语音
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AjaxResult> ConvertAudio([FromForm] string msg, [FromForm] string voice)
        {
            var uploadPath = $"webfile/TEMP/{DateTime.Now:yyyyMM}";  // 文件上传目录
            var savePath = Path.Combine(Directory.GetCurrentDirectory(), uploadPath);  // 文件保存路径
            if (!Directory.Exists(savePath)) Directory.CreateDirectory(savePath);
            var OUTPUT_FILE = DateTime.Now.Ticks + ".mp3";
            var yy = Edge_tts.GetVoice().FirstOrDefault(i => i.ShortName == voice);
            Edge_tts.SaveAudio(msg, yy, -10, $"{savePath}/{OUTPUT_FILE}");
          
            bool isTimeout = false;//最多等待10秒
            Task timeoutTask = Task.Delay(10000).ContinueWith(t => {isTimeout = true;});
            while (true)
            {
                if (isTimeout)
                {
                    break;
                }
                if (System.IO.File.Exists($"{savePath}/{OUTPUT_FILE}"))
                {
                    break;
                }
                await Task.Delay(100);
            }
            return Success($"/{uploadPath}/{OUTPUT_FILE}");
        }

    }
}
